﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Model;
using DL;

namespace DokterAfspraken
{
    class LoginFactory
    {
         public void Inloggen(string username, string passwoord, bool checkbox)
         {
             Model.DataModelManager manager = new DataModelManager();

             if (manager.getPaswoord(username) == passwoord)
             {
                 string id = manager.getLoginID(username).ToString();
                 string type = manager.getAccountType(username);
                

                 if (checkbox == true)
                 {
                     HttpCookie eenCookie = new HttpCookie("UserSettings");
                     eenCookie["dokterspraktijk_id"] = id;
                     eenCookie["dokterspraktijk_username"] = username;
                     if (type == "Arts" || type == "Admin")
                     {
                         eenCookie.Expires = DateTime.Now.AddDays(365d);
                     }
                     else
                     {
                        eenCookie.Expires = DateTime.Now.AddDays(30d);
                     }
                     System.Web.HttpContext.Current.Response.Cookies.Add(eenCookie);
                     
                 }
                 else
                 {
                     System.Web.HttpContext.Current.Session["dokterspraktijk_id"] = id;
                     System.Web.HttpContext.Current.Session["dokterspraktijk_username"] = username;
                    
                 }

                 if (type == "Arts")
                 {
                     System.Web.HttpContext.Current.Session["dokterspraktijk_artsID"] = manager.getPersoonID(int.Parse(id));
                 }

                 else if (type == "Patient")
                 {
                     System.Web.HttpContext.Current.Session["dokterspraktijk_patientID"] = manager.getPersoonID(int.Parse(id));
                 }
 
             }
             else
             {
                 System.Web.HttpContext.Current.Response.Write("Probleempje");
             }
        }
  
         public bool CheckData(int loginID, string username)
         {
             Model.DataModelManager manager = new DataModelManager();
             if (manager.checkPatientIDgebruikersnaamCorrect(loginID, username) == true)
             {
                 return true;
             }
             else
             {
                 return false;
             }
         }

         public bool CheckSessie()
         {
             if(System.Web.HttpContext.Current.Session["dokterspraktijk_id"] != null)
             {
             int loginID = int.Parse(System.Web.HttpContext.Current.Session["dokterspraktijk_id"].ToString());
             string username = System.Web.HttpContext.Current.Session["dokterspraktijk_username"].ToString();

                 if (CheckData(loginID, username) == true)
                     {
                         return true;
                     }

                 else
                    { 
                        System.Web.HttpContext.Current.Session["dokterspraktijk_id"] = null;
                        System.Web.HttpContext.Current.Session["dokterspraktijk_username"] = null;

                        return false;
                    }
             }

             else
             {
                 return false;
             }

         }

         public bool CheckCookie()
         {
             HttpCookie myCookie = new HttpCookie("UserSettings");
             myCookie = System.Web.HttpContext.Current.Request.Cookies["UserSettings"];
             
             if (myCookie != null)
             {
                 Model.DataModelManager manager = new DataModelManager();
                 // Als cookie niet leeg is
                 string username = myCookie["dokterspraktijk_username"].ToString();
                 //manager.getAccountType(username)
                 int loginID = int.Parse(myCookie["dokterspraktijk_id"].ToString());
             

                 if (CheckData(loginID, username) == true)
                     {
                         return true;
                     }

                 else
                    { 
                     myCookie.Expires = DateTime.Now.AddDays(-1d);
                     System.Web.HttpContext.Current.Response.Cookies.Add(myCookie);
                     
                     return false;
                    }
             }
             else

             {
                 return false;
             }
         }


         public bool CheckLogin()
         {
             if (CheckCookie() == true || CheckSessie() == true)
             { 
                 return true;
             }
             else
             {
                 return false;
             }
         }

         public void LoginReqAdmin()
         {
             if(CheckLogin() == false)
             {
                 System.Web.HttpContext.Current.Response.Redirect("../AdminPages/LoginScreenAdminDoctor.aspx");
             }
             else
             {
                 //Gewoon verder gaan
             }
         }


         public void LogOut()
         {
             if (CheckSessie() == true)
             {
                 System.Web.HttpContext.Current.Session["dokterspraktijk_id"] = null;
                 System.Web.HttpContext.Current.Session["dokterspraktijk_username"] = null;

             }
             if (CheckCookie() == true)
             {
                 HttpCookie myCookie = new HttpCookie("UserSettings");
                 myCookie.Expires = DateTime.Now.AddDays(-1d);
                 System.Web.HttpContext.Current.Response.Cookies.Add(myCookie);
             }

         }
     
    }
}